package com.xfn.controller;

import com.xfn.service.CarDataService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/carData")
public class CarDataController {
    @Autowired(required = false)
    private CarDataService carDataService;
    @PostMapping(value = "/carInfo")
    public JSONObject carInfo(int pageNum, int pageSize,String type,String logmin,String logmax,String retrieval){
        String  ss = carDataService.carData(pageNum,pageSize,type,logmin,logmax,retrieval);
        JSONObject jsonObject = JSONObject.fromObject(ss);
        return jsonObject;
    }
    @PostMapping(value = "/upload")
    public String upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        /*String destFileName = request.getServletContext().getRealPath("")+
                "upload"+ File.separator + fileName;*/
        String destFileName = "D:/"+
                "uploadFile"+ File.separator + fileName;
        File destFile = new File(destFileName);
        destFile.getParentFile().mkdir();
        file.transferTo(destFile);
        request.getSession().setAttribute("fileName",fileName);
        return null;
    }
    @PostMapping(value = "/addData")
    public JSONObject addData(String carName,String carName1 ,String carType ,String carNum ,String carDes,HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        String fileName = (String) request.getSession().getAttribute("fileName");
        ServletContext application = request.getServletContext();
        String userName = (String) application.getAttribute("userName");
        carDataService.addCarData(carName,carType,carNum,userName,fileName);
        jsonObject.put("status","0000");
        return jsonObject;
    }
    @PostMapping(value = "/delData")
    public JSONObject delData(String[] ids){
        JSONObject jsonObject = new JSONObject();
        List<Map<String,String>> list = new ArrayList();
        for(int i=0;i<ids.length;i++){
            Map<String,String> map = new HashMap();
            map.put("id",ids[i]);
            System.out.println("批量删除的id:"+ids[i]);
            list.add(i,map);
        }
        int i = carDataService.delData(list);
        System.out.println(i);
        jsonObject.put("status","0000");
        return jsonObject;
    }
    //出租车辆
    @PostMapping(value = "/updateCar")
    public JSONObject updateCar(String num,String idX,String numCar){
        JSONObject jsonObject = new JSONObject();
        int x = Integer.parseInt(num);
        int y = Integer.parseInt(idX);
        int z = Integer.parseInt(numCar);
        if(z<x){
            jsonObject.put("status","0000");
            jsonObject.put("resdes","输入不合法！！！");
        }else {
            carDataService.updateCar(x,y);
            carDataService.updateCarLease(x,y);
            jsonObject.put("status","999");
            jsonObject.put("resdes","出租成功！！！");
        }
        return jsonObject;
    }
    @PostMapping(value = "/leaseCar")
    public JSONObject leaseCar(int pageNum, int pageSize){
        String  ss = carDataService.leaseCar(pageNum,pageSize);
        JSONObject jsonObject = JSONObject.fromObject(ss);
        return jsonObject;
    }
    @PostMapping(value = "/returnCar")
    public JSONObject returnCar(String idX,String numCar,String price){
        int num = Integer.parseInt(numCar);
        int i = carDataService.returnCar(idX,num);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Status","0000");
        jsonObject.put("ReDes","归还成功，待支付");
        return jsonObject;
    }


    @PostMapping(value = "/paymentCar")
    public JSONObject paymentCar(int pageNum, int pageSize){
        String  ss = carDataService.paymentCar(pageNum,pageSize);
        JSONObject jsonObject = JSONObject.fromObject(ss);
        return jsonObject;
    }

    @PostMapping(value = "/pay")
    public JSONObject pay(String id,int leaseNum){
        int  ss = carDataService.payMoney(id,leaseNum);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Status","0000");
        jsonObject.put("ReDes","");
        return jsonObject;
    }
    @PostMapping(value = "/rest")
    public JSONObject rest(String password,String yPass,HttpServletRequest request){
        String pwd = (String) request.getSession().getAttribute("password");
        JSONObject jsonObject = new JSONObject();
        if(yPass.equals(pwd)){
            String username = (String) request.getSession().getAttribute("userName");
            carDataService.updatePwd(password,username);
            jsonObject.put("Status","0000");
            jsonObject.put("ResDes","更新成功");
        }else {
            jsonObject.put("Status","1111");
            jsonObject.put("ResDes","修改失败");
        }
        return jsonObject;
    }

}
